using System.Collections.Generic;
using CustomPeds.Menus;
using GTA;
using GTA.Math;

namespace CustomPeds;

public class DressingScript : Script
{
    private static Vector3 playerPoint;
    private static int camViewMode;
    private static float camPitch;
    public static List<Skin.WeaponData> weapons = new List<Skin.WeaponData>();
    public static Main.Member Dressing;

    public static void InitDressing(Main.Member member)
    {
        Dressing = member;
        camViewMode = OtherHelper.GetFollowPedCamViewMode();
        playerPoint = Game.Player.Character.Position;
        camPitch = GameplayCamera.RelativePitch;
        OtherHelper.SetFollowPedCamViewMode(1);
        GameplayCamera.RelativePitch = 0f;
        OtherHelper.SetFollowPedCamViewMode(4);
        Game.Player.Character.Position = new Vector3(-168.3103f, -297.5669f, 39.73331f);
        Game.Player.Character.Heading = 208.8544f;
        if (Dressing == Main.Member.P2)
        {
            if (Main.P2.Character.IsInGroup)
            {
                Main.P2.Character.LeaveGroup();
            }
            Main.P2.Character.Position = Game.Player.Character.GetOffsetPosition(new Vector3(0f, 2f, -1f));
            Main.P2.Character.Heading = Game.Player.Character.Heading - 180f;
            Game.Player.Character.Task.TurnTo(Main.P2.Character);
            weapons = Main.P1.skin.GetWeaponsData();
            Main.P2.Character.Weapons.RemoveAll();
        }
        else
        {
            Game.Player.Character.Clone(false);
            Ped[] nearbyPeds = World.GetNearbyPeds(Game.Player.Character.Position, 3f, Game.Player.Character.Model);
            foreach (Ped ped in nearbyPeds)
            {
                if (ped.Handle != Game.Player.Character.Handle)
                {
                    Main.Clone.Character = ped;
                }
            }
            Main.Clone.Character.Position = Game.Player.Character.GetOffsetPosition(new Vector3(0f, 2f, -1f));
            Main.Clone.Character.Heading = Game.Player.Character.Heading - 180f;
            Main.Clone.Character.BlockPermanentEvents = true;
            Main.Clone.Character.IsPersistent = true;
            Main.Clone.BuildPedData(Main.Clone.Character, Main.Member.Clone, Main.P1.skin.IsAddonPed);
            Main.Clone.skin = new Skin(Main.Clone.Character, Main.P1.skin.IsAddonPed);
            if (Main.P2.Character != null)
            {
                Main.P2.Character.Position = Game.Player.Character.GetOffsetPosition(new Vector3(1.8f, 0f, -1f));
                Main.P2.Character.Task.TurnTo(Main.Clone.Character);
            }
            Game.Player.Character.Task.TurnTo(Main.Clone.Character);
            weapons = Main.P1.skin.GetWeaponsData();
        }
        Wait(2000);
        Game.Player.Character.Task.PlayAnimation("missheistdocks2bleadinoutlsdh_2b_int", "standing_loop_wade", 8f, -4f, -1, AnimationFlags.StayInEndFrame, 1f);
        GameplayCamera.RelativePitch = -19f;
        DressingRoomMenu.InitDressingRoom();
    }

    public static void ReDressing(Main.Member member)
    {
        OtherHelper.SetFollowPedCamViewMode(1);
        GameplayCamera.RelativePitch = 0f;
        OtherHelper.SetFollowPedCamViewMode(4);
        Game.Player.Character.Position = new Vector3(-168.3103f, -297.5669f, 38.73331f);
        Game.Player.Character.Heading = 208.8544f;
        if (member == Main.Member.P2)
        {
            if (Main.P2.Character.IsInGroup)
            {
                Main.P2.Character.LeaveGroup();
            }
            Main.P2.Character.Position = Game.Player.Character.GetOffsetPosition(new Vector3(0f, 2f, -1f));
            Main.P2.Character.Heading = Game.Player.Character.Heading - 180f;
            Game.Player.Character.Task.TurnTo(Main.P2.Character);
        }
        else
        {
            Main.Clone.Character.Position = Game.Player.Character.GetOffsetPosition(new Vector3(0f, 2f, -1f));
            Main.Clone.Character.Heading = Game.Player.Character.Heading - 180f;
            Main.Clone.Character.BlockPermanentEvents = true;
            Main.Clone.Character.IsPersistent = true;
            if (Main.P2.Character != null)
            {
                Main.P2.Character.Position = Game.Player.Character.GetOffsetPosition(new Vector3(1.8f, 0f, -1f));
                Main.P2.Character.Task.TurnTo(Main.Clone.Character);
            }
            Game.Player.Character.Task.TurnTo(Main.Clone.Character);
        }
        Game.Player.Character.Task.PlayAnimation("missheistdocks2bleadinoutlsdh_2b_int", "standing_loop_wade", 8f, -4f, -1, AnimationFlags.StayInEndFrame, 1f);
        GameplayCamera.RelativePitch = -19f;
        DressingRoomMenu.InitDressingRoom();
    }

    public static void EndDressing()
    {
        if (Main.P2.Character != null && !Main.P2.Character.IsInGroup && Main.P2.Follow)
        {
            Game.Player.Character.PedGroup.Add(Main.P2.Character, leader: false);
        }
        if (Main.Clone.Character != null)
        {
            Main.Clone.Character.MarkAsNoLongerNeeded();
            Main.Clone.Character.Delete();
        }
        if (weapons != new List<Skin.WeaponData>() || weapons != null)
        {
            if (Main.P2.Character != null)
            {
                Main.P2.skin.SetWeapons(weapons);
            }
            Main.P1.skin.SetWeapons(weapons);
        }
        Game.Player.Character.Task.PlayAnimation("missheistdocks2bleadinoutlsdh_2b_int", "standing_loop_wade", 8f, -4f, -1, AnimationFlags.None, 1f);
        OtherHelper.SetFollowPedCamViewMode(camViewMode);
        Game.Player.Character.Position = playerPoint + Vector3.WorldDown;
        GameplayCamera.RelativePitch = camPitch;
    }

    public static void Distance()
    {
        if (Dressing == Main.Member.P2)
        {
            float num = Main.P2.Character.Position.DistanceTo(Main.P1.Character.Position);
            if (num >= 2.8f)
            {
                Main.P2.Character.Position = Game.Player.Character.GetOffsetPosition(new Vector3(0f, 1f, -1f));
            }
            else
            {
                Main.P2.Character.Position = Game.Player.Character.GetOffsetPosition(new Vector3(0f, num + 0.5f, -1f));
            }
        }
        else
        {
            float num2 = Main.Clone.Character.Position.DistanceTo(Main.P1.Character.Position);
            if (num2 >= 2.8f)
            {
                Main.Clone.Character.Position = Game.Player.Character.GetOffsetPosition(new Vector3(0f, 1f, -1f));
            }
            else
            {
                Main.Clone.Character.Position = Game.Player.Character.GetOffsetPosition(new Vector3(0f, num2 + 0.5f, -1f));
            }
        }
    }

    public static void TurnLeft()
    {
        if (Dressing == Main.Member.P2)
        {
            Main.P2.Character.Heading = Main.P2.Character.Heading + 30f;
        }
        else
        {
            Main.Clone.Character.Heading = Main.Clone.Character.Heading + 30f;
        }
    }

    public static void TurnRight()
    {
        if (Dressing == Main.Member.P2)
        {
            Main.P2.Character.Heading = Main.P2.Character.Heading - 30f;
        }
        else
        {
            Main.Clone.Character.Heading = Main.Clone.Character.Heading - 30f;
        }
    }

    public static void AngleUp()
    {
        if (GameplayCamera.RelativePitch >= 59f)
        {
            GameplayCamera.RelativePitch = -60f;
        }
        else
        {
            GameplayCamera.RelativePitch += 6f;
        }
    }

    public static void AngleDown()
    {
        if (GameplayCamera.RelativePitch <= -59f)
        {
            GameplayCamera.RelativePitch = 60f;
        }
        else
        {
            GameplayCamera.RelativePitch -= 6f;
        }
    }
}


